package com.yxzr.youyou.adapter;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.yxzr.youyou.R;
import com.yxzr.youyou.bean.MessageBean;
import com.yxzr.youyou.utils.DateTimeUtil;

import java.util.ArrayList;
import java.util.List;

public class MessageAdapter extends RecyclerView.Adapter<MessageAdapter.ViewHolder>  {

    private Context mContext;
    private List<MessageBean.DataBean> list = new ArrayList<>();
    public MessageAdapter(Context context){
        mContext = context;
    }

    public void addList(List<MessageBean.DataBean> mlist){
        if (mlist != null && mlist.size() > 0){
            list.addAll(mlist);
            notifyDataSetChanged();
        }
    }

    public void clearList(){
        list.clear();
        notifyDataSetChanged();
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        ViewHolder viewHolder = new ViewHolder(LayoutInflater.from(mContext).inflate(R.layout.message_adapter_layout,parent,false));
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        holder.message_tv_time.setText(DateTimeUtil.formatDateTime(Long.parseLong(list.get(position).getCreate_time())));
        holder.message_tv_title.setText(list.get(position).getTitle());
        holder.message_tv_content.setText(list.get(position).getContent());
    }

    @Override
    public int getItemCount() {
        return list.size();
    }

    class ViewHolder extends RecyclerView.ViewHolder{
        private TextView message_tv_time,message_tv_title,message_tv_content;
        public ViewHolder(View itemView) {
            super(itemView);
            message_tv_time = itemView.findViewById(R.id.message_tv_time);
            message_tv_title = itemView.findViewById(R.id.message_tv_title);
            message_tv_content = itemView.findViewById(R.id.message_tv_content);
        }
    }
}
